<script setup lang="tsx">
import { NButton, NTag } from 'naive-ui';
import { h } from 'vue';
import { useAuth } from '@/hooks/business/auth';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { $t } from '@/locales';
import { DICT_TYPE, getDictObj } from '@/utils/dict';
import SvgIcon from '@/components/custom/svg-icon.vue';
import { type LogOperate, fetchGetLogOperateList } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import OperateLoginSearch from './modules/log-operate-search.vue';
import OperateLoginDetailModel from './modules/log-operate-detail-model.vue';

const appStore = useAppStore();
const { hasAuth } = useAuth();

const { columns, data, loading, mobilePagination, getDataByPage, getData, searchParams, resetSearchParams } = useTable({
  apiFn: fetchGetLogOperateList,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    userId: null,
    userType: null,
    applicationName: null,
    duration: null,
    resultCode: null,
    operateType: null,
    beginTime: null
  },
  columns: () => [
    {
      type: 'selection'
    },
    {
      key: 'beginTime',
      title: $t('page.manage.log.operate.beginTime'),
      align: 'center',
      width: 180,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'id',
      title: $t('page.manage.log.operate.id'),
      align: 'center',
      width: 100
    },
    {
      key: 'traceId',
      title: $t('page.manage.log.operate.traceId'),
      align: 'center',
      width: 500,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'nickname',
      title: $t('page.manage.log.operate.nickname'),
      align: 'center',
      width: 100
    },
    // {
    //   key: 'userType',
    //   title: $t('page.manage.log.operate.userType'),
    //   align: 'center',
    //   width: 150,
    //   render: row => {
    //     if (row.userType === null) {
    //       return null;
    //     }
    //     const dictObj = getDictObj(DICT_TYPE.USER_TYPE, row.userType);

    //     return <NTag type={dictObj?.colorType}>{dictObj?.label}</NTag>;
    //   }
    // },
    {
      key: 'applicationName',
      title: $t('page.manage.log.operate.applicationName'),
      align: 'center',
      width: 150
    },
    {
      key: 'operateModule',
      title: $t('page.manage.log.operate.operateModule'),
      align: 'center',
      width: 180
    },
    {
      key: 'operateName',
      title: $t('page.manage.log.operate.operateName'),
      align: 'center',
      width: 180
    },
    {
      key: 'operateType',
      title: $t('page.manage.log.operate.operateType'),
      align: 'center',
      width: 180,
      render: row => {
        if (row.operateType === null) {
          return null;
        }
        const dictObj = getDictObj(DICT_TYPE.LOG_OPERATE_TYPE, row.operateType);

        return <NTag type={dictObj?.colorType}>{dictObj?.label}</NTag>;
      }
    },
    {
      key: 'requestMethod',
      title: $t('page.manage.log.operate.requestMethod'),
      align: 'center',
      width: 150
    },
    {
      key: 'requestUrl',
      title: $t('page.manage.log.operate.requestUrl'),
      align: 'center',
      width: 200
    },
    {
      key: 'resultMsg',
      title: $t('page.manage.log.operate.resultMsg'),
      align: 'center',
      width: 150
    },
    {
      key: 'duration',
      title: $t('page.manage.log.operate.duration'),
      align: 'center',
      width: 150,
      ellipsis: {
        tooltip: true
      },
      render: row => {
        if (row.duration === null) {
          return null;
        }
        return <NTag type={row.duration >= 1000 ? 'error' : 'success'}>{row.duration}</NTag>;
      }
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      fixed: 'right',
      width: 100,
      render: row => (
        <div class="flex-center gap-8px">
          <div v-show={hasAuth('system:dict_type:update')}>
            <NButton type="primary" ghost size="small" onClick={() => openDetail(row)}>
              {{
                default: () => $t('common.detail'),
                icon: () => h(SvgIcon, { icon: 'ph:eye' })
              }}
            </NButton>
          </div>
        </div>
      )
    }
  ]
});

const { modelVisible, editingData, handleEdit } = useTableOperate(data, getData);

function openDetail(row: LogOperate) {
  handleEdit(row.id);
}
</script>

<template>
  <div class="flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <OperateLoginSearch
      v-if="hasAuth('system:log-operate:query')"
      v-model:model="searchParams"
      @reset="resetSearchParams"
      @search="getDataByPage"
    />
    <NCard
      :title="$t('page.manage.log.operate.title')"
      :bordered="false"
      size="small"
      class="sm:flex-1-hidden card-wrapper"
    >
      <NDataTable
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="3088"
        :loading="loading"
        :row-key="row => row.id"
        remote
        :pagination="mobilePagination"
        class="sm:h-full"
      />

      <OperateLoginDetailModel v-model:visible="modelVisible" :row-data="editingData" />
    </NCard>
  </div>
</template>

<style scoped></style>
